package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.YhqDetailActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.VerifiedActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import java.text.DecimalFormat;
import java.util.List;

import retrofit2.Call;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class CampaignYhqAdapter extends BaseListAdapter<LinkedTreeMap> {

    public CampaignYhqAdapter(Context context, List<LinkedTreeMap> objects, String bid) {
        super(context, objects, bid);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_campaign_yhq_item, parent, false);
                holder.amount = (TextView) convertView.findViewById(R.id.amount);
                holder.type_name = (TextView) convertView.findViewById(R.id.type_name);
                holder.min_amount = (TextView) convertView.findViewById(R.id.min_amount);
                holder.branch_name = (TextView) convertView.findViewById(R.id.branch_name);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                holder.typeSwq = (TextView) convertView.findViewById(R.id.typeSwq);
                holder.nameSwq = (TextView) convertView.findViewById(R.id.nameSwq);
                holder.timeSwq = (TextView) convertView.findViewById(R.id.timeSwq);
                holder.send_number = (TextView) convertView.findViewById(R.id.send_number);
                holder.hot = (ImageView) convertView.findViewById(R.id.hot);
                holder.yhqLay = (LinearLayout) convertView.findViewById(R.id.yhqLay);
                holder.swqLay = (LinearLayout) convertView.findViewById(R.id.swqLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 1 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
//            if (position >= 3){
//                holder.hot.setVisibility(View.GONE);
//            }
            if (mDataList.get(position).get("type").equals("no")){
                if (decimalFormat.format(mDataList.get(position).get("prize_amount")).equals("0")){
                    holder.yhqLay.setVisibility(View.GONE);
                    holder.swqLay.setVisibility(View.VISIBLE);
                }else{
                    holder.yhqLay.setVisibility(View.VISIBLE);
                    holder.swqLay.setVisibility(View.GONE);
                }
            }else{
                holder.yhqLay.setVisibility(View.GONE);
                holder.swqLay.setVisibility(View.VISIBLE);
            }
            holder.type_name.setText(mDataList.get(position).get("prize_type") + "");
            holder.typeSwq.setText(mDataList.get(position).get("prize_type") + "");
            holder.amount.setText(decimalFormat.format(mDataList.get(position).get("prize_amount")));
            holder.send_number.setText("x"+decimalFormat.format(mDataList.get(position).get("send_number")));
            holder.nameSwq.setText(mDataList.get(position).get("prize_name")+"");
            holder.min_amount.setText("消费满"+decimalFormat.format(mDataList.get(position).get("prize_min_amount")) + "元可用");
            holder.branch_name.setText(mDataList.get(position).get("prize_name") + "");

            holder.time.setText("领取截止日期:"+mDataList.get(position).get("prize_get_end")+"");
            holder.timeSwq.setText("领取截止日期:"+mDataList.get(position).get("prize_get_end")+"");
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView amount, type_name, min_amount, branch_name, time, typeSwq, nameSwq, timeSwq, send_number;
        ImageView hot, ljlqImg;
        LinearLayout noDataRootLayout, yhqLay, swqLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    private void appCoupongetDetail(String MAP_ID) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getcouponbyactivity(MAP_ID, id,  BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(mContext) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(mContext, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }
            }
        });
    }

}
